package com.example.mydatasingleservice;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.example.mydatasingleservice.bean.AData;
import com.example.mydatasingleservice.bean.BData;
import com.example.mydatasingleservice.bean.CData;

public class DataSingle {
    private final static String TAG = "DataSingle";
    private DataSingle(){}
    private static DataSingle dataSingle;
    public synchronized static DataSingle getInstance(){
        if(dataSingle ==null){
            dataSingle = new DataSingle();
        }
        return dataSingle;
    }

    MutableLiveData<AData> amutableLiveData;
    MutableLiveData<BData> bmutableLiveData;
    MutableLiveData<CData> cDataMutableLiveData;

    public MutableLiveData<CData> getcDataMutableLiveData() {
        if(cDataMutableLiveData==null){
            cDataMutableLiveData = new  MutableLiveData<CData>();
        }
        return cDataMutableLiveData;
    }

    public void setcDataMutableLiveData(MutableLiveData<CData> cDataMutableLiveData) {
        this.cDataMutableLiveData = cDataMutableLiveData;
    }

    public MutableLiveData<AData> getAmutableLiveData() {
        if(amutableLiveData ==null){
            amutableLiveData = new MutableLiveData<AData>();
        }
        return amutableLiveData;
    }

    public void setAmutableLiveData(MutableLiveData<AData> amutableLiveData) {
        this.amutableLiveData = amutableLiveData;
    }

    public MutableLiveData<BData> getBmutableLiveData() {
        if(bmutableLiveData ==null){
            bmutableLiveData = new MutableLiveData<BData>();
        }
        return bmutableLiveData;
    }

    public void setBmutableLiveData(MutableLiveData<BData> bmutableLiveData) {
        this.bmutableLiveData = bmutableLiveData;
    }
    public void UPBdata(BData bData) {
        if(bmutableLiveData == null || bmutableLiveData.getValue() ==null){
            bmutableLiveData = new MutableLiveData<>();
            bmutableLiveData.postValue(bData);
        }else {
            bmutableLiveData.postValue(bData);
        }
    }
    public void UPAdata(AData aData) {
        if(amutableLiveData == null || amutableLiveData.getValue() ==null){
            amutableLiveData = new MutableLiveData<>();
            amutableLiveData.postValue(aData);
        }else {
            amutableLiveData.postValue(aData);
        }
    }
    public void changeCdata(CData.CDataState cDataState){
        Log.d(TAG, "changeCdata: "+cDataState);
        CData cData = null;
        if(cDataMutableLiveData == null || cDataMutableLiveData.getValue() ==null){
            cDataMutableLiveData = new MutableLiveData<>();
            cData = new CData();
        }else {
            cData = cDataMutableLiveData.getValue();
        }
        cData.setcDataState(cDataState);
        cDataMutableLiveData.postValue(cData);
    }
    public void changeBdata(BData.BDataState bDataState) {
        Log.d(TAG, "changeBdata: "+bDataState);

        BData bData = null;
        if(bmutableLiveData == null || bmutableLiveData.getValue() ==null){
            bmutableLiveData = new MutableLiveData<>();
            bData = new BData();
        }else {
            bData = bmutableLiveData.getValue();
        }
        bData.setbDataState(bDataState);
        bmutableLiveData.postValue(bData);
    }

    public void changeAdata(AData.ADataState aDataState) {
        Log.d(TAG, "changeAdata: "+aDataState);

        AData aData = null;
        if(amutableLiveData == null || amutableLiveData.getValue() ==null){
            amutableLiveData = new MutableLiveData<>();
            aData = new AData();
        }else {
            aData = amutableLiveData.getValue();
        }
        aData.setaDataState(aDataState);
        amutableLiveData.postValue(aData);
    }


    public boolean checkAData() {
        if(amutableLiveData == null || amutableLiveData.getValue()==null || amutableLiveData.getValue().getaDataState() == AData.ADataState.NO){
            return false;
        }
        return true;
    }
    public boolean checkBData() {
        if(bmutableLiveData == null || bmutableLiveData.getValue()==null || bmutableLiveData.getValue().getbDataState()== BData.BDataState.NO){
            return false;
        }
        return true;
    }
    public boolean checkCData() {
        if(cDataMutableLiveData == null || cDataMutableLiveData.getValue()==null || cDataMutableLiveData.getValue().getcDataState()== CData.CDataState.NO){
            return false;
        }
        return true;
    }
}
